module.exports = (sequelize, DataTypes) => {
  const Tag = sequelize.define('Tag', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      comment: '主键ID'
    },
    name: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: '标签名称'
    },
    slug: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: 'URL友好标识'
    },
    color: {
      type: DataTypes.STRING(7),
      defaultValue: '#3b82f6',
      comment: '标签颜色'
    },
    description: {
      type: DataTypes.STRING(255),
      comment: '标签描述'
    },
    usageCount: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      field: 'usage_count',
      comment: '使用次数'
    },
    isActive: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'is_active',
      comment: '是否启用'
    }
  }, {
    tableName: 'tags',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '标签表',
    indexes: [
      {
        unique: true,
        fields: ['name']
      },
      {
        unique: true,
        fields: ['slug']
      },
      {
        fields: ['usage_count']
      }
    ]
  });

  Tag.associate = (models) => {
    // 与课程的多对多关联
    Tag.belongsToMany(models.Lesson, {
      through: models.LessonTag,
      foreignKey: 'tagId',
      otherKey: 'lessonId',
      as: 'lessons'
    });
  };

  return Tag;
}; 